/**
 * JAVACC DEMO 1.0
 */
package com.apache.uct.controller;

import com.apache.api.manager.BaseManager;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultMsg;
import com.apache.api.vo.Token;
import com.apache.database.constant.Validator;
import com.apache.database.model.Page;
import com.apache.tools.DateUtils;
import com.apache.uct.common.BaseAction;
import com.apache.uct.common.entity.Sys;
import com.apache.uct.common.entity.User;
import com.apache.uct.manager.SysManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * description:  系统注册Action
 *
 * @author iussoft 创建时间：2016-3-24
 */
@Controller
@RequestMapping("uct/sys/")
public class SysAction extends BaseAction<Sys> {

    @Autowired
    private SysManager sysManager;

    @RequestMapping("sys!data.action")
    @ResponseBody
    protected Object data(HttpServletRequest request, String page, String rows) {
        ParamsVo<Sys> vo = new ParamsVo<Sys>();
        Sys sys = new Sys();
        if (Validator.isNotNull(request.getParameter("sysEname"))) {
            sys.setSysEname(request.getParameter("sysEname"));
        }
        if (Validator.isNotNull(request.getParameter("sysCname"))) {
            sys.setSysCname(request.getParameter("sysCname"));
        }
        vo.setParams("pageIndex", page);
        vo.setParams("pageSize", rows);
        vo.setObj(sys);
        Page pageList = sysManager.getPageInfo(vo);
        Map<String, Object> mapData = new HashMap<String, Object>();
        mapData.put("total", pageList.getCount());
        mapData.put("rows", pageList.getPageObjects());
        return mapData;
    }

    /**
     * 功能说明：跳转到新增页面
     */
    @Token(save = true)
    @RequestMapping(value = "add.action", method = RequestMethod.GET)
    protected ModelAndView add(HttpServletRequest request) {
        ModelAndView view = new ModelAndView(ADD);
        return view;
    }

    /**
     * 功能说明：跳转到编辑页面
     */
    @Token(save = true)
    @RequestMapping(value = "/edit.action")
    public ModelAndView edit(HttpServletRequest request, String id) {
        ParamsVo<Sys> vo = new ParamsVo<Sys>();
        vo.setInfoId(id);
        ModelAndView view = new ModelAndView(EDIT);
        view.addObject("item", getBaseManager().getInfoById(vo));
        return view;
    }

    @Token(remove = true)
    @RequestMapping(value = "/save.action", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg save(HttpServletRequest request, HttpServletResponse response,
            @ModelAttribute Sys item) {
        ResultMsg rmsg = new ResultMsg("T", "操作成功！");
        ParamsVo<Sys> vo = getParamsVo(request);
        long times = DateUtils.toLong(new Date());
        User user = this.getSessionUser(request);
        if (Validator.isNotNull(item.getSysId())) {//对系统信息修改
            item.setUpdateTime(times);
            item.setUpdateUser(user.getUserEname());
            vo.setObj(item);
            boolean mark = getBaseManager().editInfo(vo);
            if (!mark) {
                rmsg = getResultMsg(request, "系统信息修改失败！");
            }
        } else {//添加系统信息
            item.setCreateUser(user.getUserEname());
            item.setCreateTime(times);
            vo.setObj(item);
            vo.setKey("checkSysEname");
            String flag = String.valueOf(sysManager.execute(vo));
            if ("true".equals(flag)) {
                String id = getBaseManager().saveInfo(vo);
                if (Validator.isNull(id)) {
                    rmsg = getResultMsg(request, "系统信息添加失败！");
                }
            } else {
                rmsg = getResultMsg(request, "该系统标识已经存在，操作失败！");
            }
        }
        return rmsg;
    }

    //功能说明：跳转到系统访问控制管理列表页面
    @RequestMapping("lock-sys-list.action")
    public ModelAndView lockSysList(HttpServletRequest request) {
        ModelAndView view = new ModelAndView("uct/lock-sys-list");
        return view;
    }

    protected BaseManager<Sys> getBaseManager() {
        return sysManager;
    }

    protected String getPrefix() {
        return "uct/sys-";
    }

}
